//
//  MBProfileAboutView.m
//  Meboo
//
//  Created by kyo on 2024/3/13.
//

#import "MBProfileAboutView.h"

@interface MBProfileAboutView ()
<
UITextViewDelegate
>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *placeLab;
@end
@implementation MBProfileAboutView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

#pragma mark - Private Method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    //text为输入中的内容的最后一个字符
    ////判断输入的字是否是回车，即按下【Return】
    if ([text isEqualToString:@"\n"]){
        //在这里做你响应【Return】键的代码
        
        //一般通常也会收键盘，即取消textView的第一响应者
        [self.aboutMeView resignFirstResponder];
        /**这里返回NO，就代表【Return】键值失效，即在页面上按下
      【Return】键，不会出现换行，如果为YES，则输入页面会换行*/
        return NO;
    }

    return YES;
}

#pragma mark - Action

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 500) {
        textView.text = [textView.text substringToIndex:500];
    }
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
    }];
    
    [self addSubview:self.aboutMeView];
    [self.aboutMeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(MBHeightScale(32));
        make.left.bottom.right.equalTo(self);
    }];
    
    [self.placeLab sizeToFit];
    [self.aboutMeView addSubview:self.placeLab];
    [self.aboutMeView setValue:self.placeLab forKey:@"_placeholderLabel"];
    
    self.aboutMeView.text = [MBUserAccount defaultAccount].aboutMe;
}

#pragma mark - Setter && Getter
- (UILabel *)placeLab {
    if (!_placeLab) {
        _placeLab = [UILabel new];
        _placeLab.numberOfLines = 0;
        _placeLab.text = @"Say more about yourself";
        _placeLab.textColor = HEXS(@"999999");
        _placeLab.font = PDRegularScale(14);
    }
    return _placeLab;
}

- (UITextView *)aboutMeView {
    if (!_aboutMeView) {
        _aboutMeView = [UITextView new];
        _aboutMeView.scrollEnabled = YES;
        _aboutMeView.textColor = HEXS(@"333333");
        _aboutMeView.textContainerInset = UIEdgeInsetsMake(10, 10, 10, 10);
        _aboutMeView.delegate = self;
        _aboutMeView.backgroundColor = HEXS(@"ffffff");
        _aboutMeView.layer.cornerRadius = MBHeightScale(12);
        _aboutMeView.font = PDRegularScale(14);
    }
    return _aboutMeView;
}

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = PDSemiBoldScale(18);
        _titleLab.textColor = HEXS(@"333333");
        _titleLab.text = @"About me";
    }
    return _titleLab;
}

@end
